# -*- coding:utf-8 -*-
from infrastructure.utils.result import R
from interface.interface_base import app, request, jsonify
from service.marketSentimentService import marketSentiment, deleteMarketSentiment
from service.groupService import groupRotation, deleteGroupRotation
from service.marketSentimentViewService import sentimentViewMain
from service.stockBoardService import stockBoard, deleteSB
from infrastructure.utils.log import logger


@app.route('/marketSentiment/save', methods=['POST'])
def msSave():
    d = request.json
    marketSentiment(d)


@app.route('/marketSentiment/allDataSave', methods=['POST'])
def allDataSave():
    logger.info("开始持久化数据")
    d = request.json
    dti = None
    if 'dti' in d:
        dti = d['dti']
        logger.info(f"设定持久化数据的日期为{dti}")
    stockBoard(dti)
    groupRotation(dti)
    marketSentiment(d)
    return jsonify(R.success().__dict__)

@app.route('/marketSentiment/delAllData', methods=['POST'])
def delAllData():
    request_data = request.json
    dti = request_data.get('dti') if request_data else None
    deleteSB(dti)
    deleteGroupRotation(dti)
    deleteMarketSentiment(dti)
    return jsonify(R.success().__dict__)


@app.route('/marketSentiment/report', methods=['POST'])
def msReport():
    sentimentViewMain()
    return jsonify(R.success().__dict__)
